<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset id="searchFieldset_currentTable" class="table-search-fieldset layui-hide">
            <legend>{:lang("common.condition_search")}</legend>
            <form class="layui-form layui-form-pane form-search">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">{:lang("common.start_month")}</label>
                    <div class="layui-input-inline">
                        <input name="d_progress_date_ge" class="layui-input cssDate"
                               placeholder="{:lang('common.choose')}{:lang('common.start_month')}"
                               value="{$param.d_progress_date_ge|default=''}">
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">{:lang("common.end_month")}</label>
                    <div class="layui-input-inline">
                        <input name="d_progress_date_le" class="layui-input cssMonth"
                               placeholder="{:lang('common.choose')}{:lang('common.end_month')}"
                               value="{$param.d_progress_date_le|default=''}">
                    </div>
                </div>

                {if !auth('roleAccounting')}
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">{:lang("accounting.name")}</label>
                    <div class="layui-input-inline">
                        <select name="d_accounting_id_eq" lay-search>
                            <option value="">{:lang("common.choose")}{:lang("accounting.name")}</option>
                            {foreach :getAccountingList() as $val}
                            <option value="{$val.id}">{$val.name}</option>
                            {/foreach}
                        </select>
                    </div>
                </div>
                {/if}

                <div class="layui-form-item layui-inline" style="margin-left:50px">
                    <button type="submit" class="layui-btn layui-btn-normal" data-type="tableSearch"
                            lay-submit lay-filter="search_currentTable">{:lang("common.btn_search")}
                    </button>
                </div>
            </form>
        </fieldset>

        <table id="currentTable"
               data-auth-generate="{:auth(request()->controller().'/generateTomorrowPlan')}"
               data-auth-generate-summary="{:auth('tally.Summary/generateEmpty')}"
               data-auth-add="{:auth(request()->controller().'/add')}"
               data-auth-edit="{:auth(request()->controller().'/edit')}"
               data-auth-delete="{:auth(request()->controller().'/delete')}"
               data-auth-print="{:auth(request()->controller().'/print')}"
               data-auth-export="{:auth(request()->controller().'/export')}">
        </table>
    </div>
</div>
<script>
    layui.use(['myApp'], function () {
        var myCommon = layui.myCommon;
        window.editDay = function (id) {
            popupWindow('{:lang("day.generate_title")}', 'tally.progress_day/edit?id=' + id, function () {
                myCommon.api.refreshTable();
            }, 1200);
        }
        window.editSummary = function (id) {
            popupWindow('{:lang("day.generate_summary_title")}', 'tally.summary/edit?id=' + id, function () {
                myCommon.api.refreshTable();
            }, 800);
        }
        window.generateTomorrowPlan = function () {
            myCommon.request.post({
                url: 'tally.progress_day/generateTomorrowPlan'
                , prefix: true
            }, function (res) {
                editDay(res.data.id);
            });
        };
        window.generateSummary = function () {
            myCommon.request.post({
                url: 'tally.summary/generateEmpty'
                , prefix: true
            }, function (res) {
                editSummary(res.data.id);
            });
        };

        var columns = [
            {field: 'id', width: 60, title: '{:lang("common.id")}', sort: true}
            , {field: 'accounting_name', width: 105, title: '{:lang("accounting.name")}', sort: true}
            , {
                field: 'progress_date', width: 110, title: '{:lang("day.progress_date")}', sort: true
                , templet: myCommon.table.fn, fn: function (value, data) {
                    let id = myCommon.value(data, 'id');
                    if (myCommon.value(data, 'type') == '{$T_SUMMARY}') {
                        return '<a onclick="return editSummary(' + id + ')">{:lang("day.summary")}</a>';
                    }
                    return '<a onclick="return editDay(' + id + ')">'
                        + value + Date.parseDate(value, '%Y-%m-%d').print('[%a]') + '</a>';
                }
            }
            , {field: '_content', minWidth: 100, title: '{:lang("day.content")}'}
            , {
                field: 'estimate_time', width: 90, title: '{:lang("common.estimate_time_alias")}', sort: true
                , templet: myCommon.table.fn, fn: function (value, data) {
                    return myCommon.value(data, 'type') == '{$T_SUMMARY}' ? '' : value;
                }
            }
            , {
                field: 'actual_time', width: 90, title: '{:lang("common.actual_time_alias")}', sort: true
                , templet: myCommon.table.fn, fn: function (value, data) {
                    return myCommon.value(data, 'type') == '{$T_SUMMARY}' ? '' : value;
                }
            }
            , {
                field: 'completion_status', width: 60, title: '{:lang("day.completion_status")}', sort: true,
                templet: myCommon.table.fn, typeList: 'completion_status_list', fn: function (value, data, option) {
                    return myCommon.value(data, 'type') == '{$T_SUMMARY}' ? '' : myCommon.table.type(data, option);
                }
            }
            , {
                field: 'appraise', width: 60, title: '{:lang("common.appraise")}', sort: true,
                templet: myCommon.table.fn, typeList: 'appraise_list', fn: function (value, data, option) {
                    return myCommon.value(data, 'type') == '{$T_SUMMARY}' ? '' : myCommon.table.type(data, option);
                }
            }
            , {field: 'update_time', width: 148, title: '{:lang("common.update_time")}', sort: true}
        ];
        if (myCommon.checkAuth('delete', '#currentTable')) {
            columns.push({
                width: 50, title: '{:lang("common.operate")}', templet: myCommon.table.tool, operat: ['delete']
            });
        }
        myCommon.table.render({
            where: myCommon.generateCondition()
            , totalRow: true
            , limit: "{$param.limit|default='15'}"
            , cols: [columns]
            , toolbar: ['refresh', {
                auth: 'generate'
                , text: '{:lang("day.btn_generate")}'
                , class: 'layui-btn layui-btn-normal layui-btn-sm'
                , icon: 'fa fa-gears'
                , fn: function () {
                    return 'onclick="generateTomorrowPlan()"';
                }
            }, {
                auth: 'generate-summary'
                , text: '{:lang("day.btn_generate_summary")}'
                , class: 'layui-btn layui-btn-normal layui-btn-sm'
                , icon: 'fa fa-gears'
                , fn: function () {
                    return 'onclick="generateSummary()"';
                }
            }, 'export']
            , done: function (res) {
                myCommon.tableTotal(this, res.data, ['estimate_time', 'actual_time']);
            }
        });
        myCommon.listen();
    });
</script>
